package com.gjzt.business.depositOrder.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.gjzt.business.bid.service.BiddingDepositService;
import com.gjzt.business.utils.Constant;
import com.unionpay.acp.sdk.AcpService;
import com.unionpay.acp.sdk.LogUtil;
import com.unionpay.acp.service.PayTools;
import com.unionpay.acp.service.VerifyOrderService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 订单校验定时器(间隔时长：一小时)
 * @author zwm
 *
 */
@Service
public class VerifyDepositOrderTaskService extends BaseService {

	@Autowired
	private VerifyOrderService verifyOrderService;
	@Autowired
	private DepositOrderService depositOrderService;
	@Autowired
	private BiddingDepositService biddingDepositService;

	public void verifyDepositOrder() {

		// 查询所有没有支付成功的订单
		String sql = "SELECT adr.*  from  AD_DEPOSIT_ORDER  adr  where adr.ORDER_STATUS <>"
				+ Constant.DEPOSIT_ORDER_STATUS_PAY_SUCCESS;
		JSONArray orderArray = this.query(new SqlBuilder(sql));

		for (int i = 0; i < orderArray.size(); i++) {
			JSONObject json = orderArray.getJSONObject(i);
			String orderId = json.getString("orderId");
			String txnTime = json.getString("txnTime");
			String customerId = json.getString("customerId");
			String bidId = json.getString("bidId");

			Map<String, String> rspData = verifyOrderService.getVerifyOrderInfo(orderId, txnTime);

			String orderDesc = "";// 校验提示信息
			if (!rspData.isEmpty()) {
				if (AcpService.validate(rspData, PayTools.encoding_UTF8)) {
					LogUtil.writeLog("验证签名成功");
					if ("00".equals(rspData.get("respCode"))) {// 如果查询交易成功
						// 处理被查询交易的应答码逻辑
						String origRespCode = rspData.get("origRespCode");
						if ("00".equals(origRespCode)) {
							// 交易成功，更新商户订单状态
							orderDesc = "支付成功";
							depositOrderService.updateOrder(rspData, Constant.DEPOSIT_ORDER_STATUS_PAY_SUCCESS,
									orderDesc);
							//支付成功  ,插入客户保证金记录
							biddingDepositService.payDeposit(customerId, bidId);
							
						} else if ("03".equals(origRespCode) || "04".equals(origRespCode)
								|| "05".equals(origRespCode)) {
							// 需再次发起交易状态查询交易  支付中
							depositOrderService.updateOrderStatus(orderId, Constant.DEPOSIT_ORDER_STATUS_PAY_ING, "支付中");
							LogUtil.writeLog("订单状态====> " + origRespCode);
						} else {
							// 其他应答码为失败请排查原因
							depositOrderService.updateOrderStatus(orderId, Constant.DEPOSIT_ORDER_STATUS_PAY_FAIL, rspData.get("origRespMsg"));
							LogUtil.writeLog("订单状态====> " + origRespCode + " 原因 ===> " + rspData.get("origRespMsg"));
						}
					} else {// 查询交易本身失败，或者未查到原交易，检查查询交易报文要素
						LogUtil.writeLog("查询交易本身失败====> " + rspData.get("respMsg"));
					}
				} else {
					LogUtil.writeErrorLog("检查验证签名失败");
					// TODO 检查验证签名失败的原因
				}
			} else {
				// 未返回正确的http状态
				LogUtil.writeErrorLog("未获取到返回报文或返回http状态码非200");
			}
		}
	}
}